package org.dromara.merchants.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.merchants.domain.TaGoods;
import org.dromara.merchants.domain.bo.TaGoodsBo;
import org.springframework.stereotype.Service;
import org.dromara.merchants.domain.bo.TaStoreBo;
import org.dromara.merchants.domain.vo.TaStoreVo;
import org.dromara.merchants.domain.TaStore;
import org.dromara.merchants.mapper.TaStoreMapper;
import org.dromara.merchants.service.ITaStoreService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 店铺Service业务层处理
 *
 * @author xiyin321
 * @date 2024-07-02
 */
@RequiredArgsConstructor
@Service
public class TaStoreServiceImpl implements ITaStoreService {

    private final TaStoreMapper baseMapper;

    /**
     * 查询店铺
     */
    @Override
    public TaStoreVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询店铺列表
     */
    @Override
    public TableDataInfo<TaStoreVo> queryPageList(TaStoreBo bo, PageQuery pageQuery) {
        Page<TaStoreVo> result = baseMapper.selectStorePage(pageQuery.build(), this.buildQueryWrapper(bo));
        return TableDataInfo.build(result);
    }

    /**
     * app端查询店铺列表
     *
     * @param bo
     * @param pageQuery
     */
    @Override
    public TableDataInfo<TaStoreVo> queryAppList(TaStoreBo bo, PageQuery pageQuery) {
        Page<TaStoreVo> result = baseMapper.selectVoPage(pageQuery.build(), this.buildLambdaQueryWrapper(bo));
        return TableDataInfo.build(result);
    }

    /**
     * 查询店铺列表
     */
    @Override
    public List<TaStoreVo> queryList(TaStoreBo bo) {
        return baseMapper.selectVoList(this.buildQueryWrapper(bo));
    }

    /**
     * 查询店铺下拉列表
     */
    @Override
    public List<TaStoreVo> queryOptionList() {
        return baseMapper.selectOptionList();
    }
    /*构建查询条件*/
    private Wrapper<TaStore> buildQueryWrapper(TaStoreBo bo) {
        QueryWrapper<TaStore> wrapper = Wrappers.query();
        wrapper.eq("s.del_flag", "0")
            .eq(StringUtils.isNotBlank(bo.getStoreName()), "s.store_name", bo.getStoreName())
            .in(ObjectUtil.isNotNull(bo.getProvinceList()), "s.province",  bo.getProvinceList())
            .in(ObjectUtil.isNotNull(bo.getCityList()), "s.city",  bo.getCityList())
            .eq(StringUtils.isNotBlank(bo.getStatus()), "s.status", bo.getStatus());
        return wrapper;
    }
    private LambdaQueryWrapper<TaStore> buildLambdaQueryWrapper(TaStoreBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TaStore> lqw = Wrappers.lambdaQuery();
        lqw.
            like(StringUtils.isNotBlank(bo.getStoreName()), TaStore::getStoreName, bo.getStoreName())
            .in(ObjectUtil.isNotNull(bo.getProvinceList()), TaStore::getProvince, bo.getProvinceList())
            .in(ObjectUtil.isNotNull(bo.getCityList()), TaStore::getCity, bo.getCityList())
            .eq(TaStore::getStatus,'0');
        return lqw;
    }

    /**
     * 新增店铺
     */
    @Override
    public Boolean insertByBo(TaStoreBo bo) {
        TaStore add = MapstructUtils.convert(bo, TaStore.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setStoreId(add.getStoreId());
        }
        return flag;
    }

    /**
     * 修改商家
     */
    @Override
    public Boolean updateByBo(TaStoreBo bo) {
        TaStore update = MapstructUtils.convert(bo, TaStore.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TaStore entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商家
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
